package cn.cl.gulimall.product.service.impl;

import cn.cl.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cl.common.utils.PageUtils;
import cn.cl.common.utils.Query;

import cn.cl.gulimall.product.dao.CategoryDao;
import cn.cl.gulimall.product.entity.CategoryEntity;
import cn.cl.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

	// @Autowired
	// private CategoryDao categoryDao;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<CategoryEntity> page = this.page(
				new Query<CategoryEntity>().getPage(params),
				new QueryWrapper<CategoryEntity>()
		);

		return new PageUtils(page);
	}

	/**
	 * 查出所有分类以及子分类，以树形结构组装起来
	 */
	@Override
	public List<CategoryEntity> listWithTree() {
		// 1. 查出所有分类
		List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
		// 2. 组装成父子树形结构
		// 2.1 找到所有的一级分类
		List<CategoryEntity> level1 = categoryEntities
				.stream()
				.filter((categoryEntity) -> categoryEntity.getParentCid() == 0)
				.map((menu) -> {
					menu.setChildren(getChildrens(menu, categoryEntities));
					return menu;
				})
				// 菜单的排序
				.sorted((menu1, menu2) -> {
					return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
				})
				.collect(Collectors.toList());
		return level1;
	}

	/**
	 * 递归查找所有菜单的子菜单
	 */
	private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all) {
		List<CategoryEntity> categoryEntities = all.stream().filter(categoryEntity -> {
			return categoryEntity.getParentCid() == root.getCatId();
		}).map(categoryEntity -> {
			// 递归 找到子菜单
			categoryEntity.setChildren(getChildrens(categoryEntity, all));
			return categoryEntity;
		}).sorted((menu1, menu2) -> {
			// 菜单的排序
			return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
		}).collect(Collectors.toList());
		return categoryEntities;
	}

	/**
	 * 批量删除分类
	 */
	@Override
	public void removeMenuByIds(List<Long> asList) {
		// TODO 1、检查当前删除的菜单是否被别的地方引用
		// 逻辑删除
		baseMapper.deleteBatchIds(asList);
	}

	/**
	 * 找到catelogId的完整路径
	 */
	@Override
	public Long[] findCatelogPath(Long catelogId) {
		if (catelogId == null) {
			return null;
		}
		List<Long> paths = new ArrayList<>();
		List<Long> parentPath = findParentPath(catelogId, paths);
		Collections.reverse(parentPath);
		return parentPath.toArray(new Long[parentPath.size()]);
	}

	/**
	 * 递归查找catelogId的完整路径
	 */
	private List<Long> findParentPath(Long catelogId, List<Long> paths) {
		// 1、收集当前节点的ID
		paths.add(catelogId);
		CategoryEntity categoryEntity = this.getById(catelogId);
		if (categoryEntity.getParentCid() != 0) {
			findParentPath(categoryEntity.getParentCid(), paths);
		}
		return paths;
	}

	@Autowired
	private CategoryBrandRelationService categoryBrandRelationService;

	/**
	 * 分类级联修改
	 */
	@Transactional
	@Override
	public void updateCascade(CategoryEntity category) {
		this.updateById(category);
		if (!StringUtils.isEmpty(category.getName())) {
			categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
		}
	}
}